#!/usr/bin/env Rscript

library(tidyverse)

brasil <- read_csv2("../app/data/brasil.csv")
data <- read_csv2("../app/data/dados_cat.csv", na = "NA",
                     col_types = cols(
                         st_acidente_feriado = col_character(),
                         ds_agente_causador = col_character(),
                         ano_cat = col_integer(),
                         ds_cnae_classe_cat = col_character(),
                         dt_acidente = col_date(format = "%d/%m/%Y"),
                         st_dia_semana_acidente = col_character(),
                         ds_emitente_cat = col_character(),
                         hora_acidente = col_time(format = "%H%M"),
                         idade_cat = col_integer(),
                         cd_indica_obito = col_character(),
                         nm_municipio = col_character(),
                         nome_uf = col_character(),
                         ds_natureza_lesao = col_character(),
                         ds_cbo = col_character(),
                         ds_parte_corpo_atingida = col_character(),
                         cd_tipo_sexo_empregado_cat = col_character(),
                         ds_tipo_acidente = col_character(),
                         ds_tipo_local_acidente = col_character()
                     ))

# Use better variable names for dataset and put locality data in front
data <- rename(data, uf = nome_uf,
                  municipio = nm_municipio) %>%
    select(uf, municipio, everything())

# Add correponding locality data from brasil to data
complete <- brasil %>% inner_join(data, by = c("uf", "municipio"))
write_delim(complete, "../app/data/completo.csv", delim = ";")

# Number of accidents:
country <- group_by(complete, pais) %>% summarize(acidentes = n())
by_region <- group_by(complete, regiao) %>% summarize(acidentes = n())
by_uf <- group_by(complete, uf) %>% summarize(acidentes = n())
by_meso <- group_by(complete, mesorregiao) %>% summarize(acidentes = n())
by_micro <- group_by(complete, microrregiao) %>% summarize(acidentes = n())
by_town <- group_by(complete, uf, municipio) %>% summarize(acidentes = n())

# Put every accident alongside locality (this is temporary)
acidentes <- brasil %>% inner_join(country, by = c("pais")) %>%
    inner_join(by_region, by = c("regiao")) %>%
    inner_join(by_uf, by = c("uf")) %>%
    inner_join(by_meso, by = c("mesorregiao")) %>%
    inner_join(by_micro, by = c("microrregiao")) %>%
    inner_join(by_town, by = c("uf", "municipio")) %>%
    select(pais,
           total = acidentes.x,
           regiao,
           acidentes_regiao = acidentes.y,
           uf,
           acidentes_uf = acidentes.x.x,
           mesorregiao,
           acidentes_meso = acidentes.y.y,
           microrregiao,
           acidentes_micro = acidentes.x.x.x,
           municipio,
           acidentes_municipio = acidentes.y.y.y)

write_delim(acidentes, "../app/data/acidentes.csv", delim = ";")

#Summarization by ds_tipo_local_acidente(axis) and ano_cat(draw lines) for radarchart
#by_local <- data %>% group_by(ano_cat, ds_tipo_local_acidente) %>% summarize(acidentes = n())

#b2012 <- by_local %>% filter(ano_cat == 2012) %>%
#    mutate(total = sum(acidentes), porcentagem = acidentes / total)

#b2013 <- by_local %>% filter(ano_cat == 2013) %>%
#    mutate(total = sum(acidentes), porcentagem = acidentes / total)

#b2014 <- by_local %>% filter(ano_cat == 2014) %>%
#    mutate(total = sum(acidentes), porcentagem = acidentes / total)

#b2015 <- by_local %>% filter(ano_cat == 2015) %>%
#    mutate(total = sum(acidentes), porcentagem = acidentes / total)

#b2016 <- by_local %>% filter(ano_cat == 2016) %>%
#    mutate(total = sum(acidentes), porcentagem = acidentes / total)

#Summarization by ano_cat(axis) and sex(draw lines) for radarchart
#Uncomment if necessary

#by_sex <- data %>% group_by(ano_cat, cd_tipo_sexo_empregado_cat) %>% summarize(acidentes = n())

#s2012 <- by_sex %>% filter(ano_cat == 2012) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)

#s2013 <- by_sex %>% filter(ano_cat == 2013) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)

#s2014 <- by_sex %>% filter(ano_cat == 2014) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)

#s2015 <- by_sex %>% filter(ano_cat == 2015) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)

#s2016 <- by_sex %>% filter(ano_cat == 2016) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)

#Sumarization of places by ano_cat and acidentes.
by_uf_municipio <- complete %>% group_by(uf, municipio, ano_cat) %>% summarize(acidentes = n())
write_delim(by_uf_municipio, "../app/data/uf-municipio.csv", delim = ";")
